<?php

class Category extends AppModel {

    var $name = 'Category';
    var $displayField = 'category_name';
    var $hasMany = array('Sizetitle' => array(
            array(
                'className' => 'Sizetitle',
                'foreignKey' => 'category_id',
                'dependent' => true
            )
        ),
        'Categorybrand' => array(
            array('className' => 'Categorybrand',
                'foreignKey' => 'category_id',
                'dependent' => true
            )
        ),
        'Product' => array(
            array('className' => 'Product',
                'foreignKey' => 'category_id'
            )
        ), 'Field'
    );
    var $validate = array(
        'category_name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Enter a category name',
            ),
        )
    );
    static $j = 0;
    var $returnArray = array();
    var $return = array();

    function categoryLeftMenu($id=NULL) {
        $this->recursive = -1;
        $item = $this->findAllByParentId($id);
        return $item;
    }

    function categoryArray($id=null) {
        if (!$id)
            $itemlist = $this->categoryLeftMenu('0');
        else
            $itemlist=$this->categoryLeftMenu($id);
        $i = 0;
        
        foreach ($itemlist as $item) {

            $itemlist[$i]['child'] = $this->categoryLeftMenu($item['Category']['id']);
            if (is_array($itemlist[$i]['child'])) {
                $j = 0;
                foreach ($itemlist[$i]['child'] as $itemschild) {
                    $itemlist[$i]['child'][$j]['child1'] = $this->categoryLeftMenu($itemschild['Category']['id']);
                    $j++;
                }
            }
            $i++;
        }
        //pr($item);
        //exit('gjhghj');
        return $itemlist;
    }

    function randomCategory() {
        $data = array(
            'conditions' => array('Category.parent_id' => '0'),
            'fields' => array('category_name', 'id'),
            'order' => array('RAND()'),
            'limit' => 2
        );
        return $this->find('all', $data);
    }

    function viewAll($parentId=0, $i=0) {
        $i++;
        $this->recursive = -1;
        $result = $this->findAllByParentId($parentId);
        foreach ($result as $row) {
            $parentId = $row['Category']['id'];
            self::$j++;
            $resultArray = array('id' => $row['Category']['id'], 'category_name' => $row['Category']['category_name'], 'parent_id' => $row['Category']['parent_id'], 'depth' => $i);
            $this->returnArray[self::$j] = $resultArray;
            $this->viewAll($parentId, $i);
        }
        return $this->returnArray;
    }

    function chkDuplicateCategory($name, $id=null, $id2=null) {
        if (empty($id2)) {
            if (empty($id)) {
                return $this->find("count", array('conditions' => array('Category.parent_id' => 0, 'Category.category_name' => $name)));
            } else {
                return $this->find("count", array('conditions' => array('Category.parent_id' => $id, 'Category.category_name' => $name)));
            }
        } else {
            if (empty($id)) {
                return $this->find("count", array('conditions' => array('Category.id !=' => $id2, 'Category.parent_id' => 0, 'Category.category_name' => $name)));
            } else {
                return $this->find("count", array('conditions' => array('Category.id !=' => $id2, 'Category.parent_id' => $id, 'Category.category_name' => $name)));
            }
        }
    }

    function total_subcategories($id = null) {
        $this->recursive = -1;
        return $this->find('count', array('conditions' => array('Category.parent_id' => $id)));
    }

    function fetchSubCategory($id=null) {
        $this->recursive = -1;
        return $this->find('all', array(
            'fields' => array('id', 'category_name'),
            'conditions' => array('Category.parent_id' => $id),
            'order' => 'Category.category_order ASC'));
    }

    function fetchParentCategory($id=null) {
        $this->recursive = -1;
        return $this->find('all', array(
            'fields' => array('id', 'category_name'),
            'conditions' => array('Category.parent_id' => '0'),
            'order' => 'Category.category_order ASC'));
    }

    function findParentsId($id=null) {
        $this->recursive = -1;
        $prent_id = $this->findById($id, array('fields' => 'parent_id'));
        return $prent_id['Category']['parent_id'];
    }

    function allchilds($parentId) {
        $result = $this->findAllByParentId($parentId);
        $i = 0;
        $ids[] = $parentId;
        foreach ($result as $row) {
            $ids[] = $row['Category']['id'];
            $result1 = $this->findAllByParentId($row['Category']['id']);
            foreach ($result1 as $row1) {
                $ids[] = $row1['Category']['id'];
                $result2 = $this->findAllByParentId($row1['Category']['id']);
                foreach ($result2 as $row2) {
                    $ids[] = $row2['Category']['id'];
                }
            }
        }
        return $ids;
    }

}

?>